/*
 * @Author: sunzhijun 
 * @Date: 2019-04-24 10:22:37 
 * @Last Modified by: sunzhijun
 * @Last Modified time: 2019-12-12 14:41:54
 */
import CommonAction from './common';

export default class TreeAction extends CommonAction{
    constructor(comp) {
        super();
        this.comp = comp;
        this.dispatch = this.comp.props.dispatch;
    }

    //获取Tree数据
    getTreeData = async ()=> {
        const { props, action } = this.comp;
        const {dispatch, main, syncTree, button} = props;
        let res = await dispatch({
            type: 'main/getTreeData'
        });
        if (res.success) {
            let data = res.data;
            if(!!data){
                this.customTree(data);
            }else{
                this.customTree();
            }
            await this.dispatch({
                type: 'main/update',
                payload: {
                    isVisiblePagination: false
                }
            })
        }
    }

    //自定义Tree数据
    customTree = (data)=>{
        const { props } = this.comp;
        const {dispatch, main, syncTree} = props;
        const { language } = main;
        let refpk = data && data[0].refpk;
        let root = [{//树根节点
            "children": [],
            "select": false,
            "key": "ROOT",
            "title": language['hi6007-000065'],
            "id": "ROOT",
            "innercode": "ROOT",
            "pid": "",
            "refname": language['hi6007-000065'],
            "refpk": "ROOT"
        }];
        Array.prototype.push.apply(root[0].children,data);
        syncTree.setSyncTreeData('treeid', root);
    }

    
    //选择节点获取相应数据
    selectTreeData = async (value,v,v1,v2)=> {
        const { props, action } = this.comp;
        const { syncTree, button, main } = props;
        let selectVal = syncTree.getSelectNode('treeid');
        await this.dispatch({
            type: 'main/update',
            payload: {
                tree_node_id: selectVal.refpk
            }
        })
        action.tableAct.getMainTableData();
        action.headerAct.setShowOrHidden(false);
    }
}
